package com.relatev.minecraft.UWantRestart.Sponge;

import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.text.Text;

public class SPRestartTasker {

    public static void restart(String tipmessage, int second) {

        Sponge.getScheduler().createTaskBuilder().interval(1, TimeUnit.SECONDS).execute(new Runnable() {

            private int count = 0;

            @Override
            public void run() {
                for (Player player : Sponge.getServer().getOnlinePlayers()) {
                    player.sendMessage(Text.of(tipmessage));
                }
                if(count >= second){
                    restart(tipmessage);
                }
                count++;
            }
        }).submit(UWantRestartSP.MainPlugin);
    }

    public static void restart(String tipmessage) {
        Iterator<? extends Player> it = Sponge.getServer().getOnlinePlayers().iterator();
        while (it.hasNext()) {
            Player player = it.next();
            player.kick(Text.of(tipmessage));
        }
        Sponge.getServer().shutdown(Text.of(tipmessage));
    }
}
